home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / cg.lha / cg / src / Optimize.mi < prev    next >
Text File  |  1992-11-24  |  7KB  |  340 lines

  1. IMPLEMENTATION MODULE Optimize;
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. IMPORT SYSTEM, System, IO, Tree;
  50. (* line 20 "" *)
  51.  
  52.  
  53. FROM SYSTEM    IMPORT TSIZE;
  54. FROM General    IMPORT Max;
  55. FROM DynArray    IMPORT MakeArray, ReleaseArray;
  56. FROM IO        IMPORT StdOutput, WriteI, WriteS, WriteNl;
  57. FROM Idents    IMPORT WriteIdent;
  58.  
  59. FROM Sets    IMPORT
  60.    tSet        , IsElement    , AssignElmt    , Union        ,
  61.    Assign    , Minimum    , Maximum    , Include    ;
  62.  
  63. FROM Relations    IMPORT
  64.    IsRelated    ;
  65.  
  66. FROM Tree    IMPORT
  67.    NoTree    , tTree        , tInstance    ,
  68.    Computed    , Reverse    , Write        , Read        ,
  69.    Inherited    , Synthesized    , Input        , Output    ,
  70.    Stack    , Parameter    , Variable    ,
  71.    CopyDef    , CopyUse    , Thread    , Test        ,
  72.    Left        , Right        , Def        , Use        ,
  73.    ChildUse    , ParentUse    , NonBaseComp    , First        ,
  74.    Dummy    , Virtual    , Demand    , f        ,
  75.    WriteName    , Options    , ForallClasses    , ForallAttributes;
  76.  
  77. FROM Order    IMPORT WriteOrderEval, IndexToClass;
  78.  
  79. TYPE tLife    = RECORD Birth, Death: SHORTCARD; END;
  80.  
  81. VAR
  82.    Children    ,
  83.    Parents    ,
  84.    Relevant    : tSet;
  85.    ClassIndex    ,
  86.    MaxChildUse    ,
  87.    MaxParentUse    ,
  88.    Attr        ,
  89.    Comp        ,
  90.    Last        ,
  91.    ChildsVisit    ,
  92.    i, i2, j, j2, k, Visit, v    : SHORTCARD;
  93.    ActChild    ,
  94.    ActClass    ,
  95.    ChildsClass    : tTree;
  96.    LifeSize    : LONGINT;
  97.    LifePtr    : POINTER TO ARRAY [0 .. 10000] OF tLife;
  98.  
  99.  
  100. PROCEDURE yyAbort (yyFunction: ARRAY OF CHAR);
  101.  BEGIN
  102.   IO.WriteS (IO.StdError, 'Error: module Optimize, routine ');
  103.   IO.WriteS (IO.StdError, yyFunction);
  104.   IO.WriteS (IO.StdError, ' failed');
  105.   IO.WriteNl (IO.StdError);
  106.   Exit;
  107.  END yyAbort;
  108.  
  109. PROCEDURE yyIsEqual (yya, yyb: ARRAY OF SYSTEM.BYTE): BOOLEAN;
  110.  VAR yyi    : INTEGER;
  111.  BEGIN
  112.   FOR yyi := 0 TO INTEGER (HIGH (yya)) DO
  113.    IF yya [yyi] # yyb [yyi] THEN RETURN FALSE; END;
  114.   END;
  115.   RETURN TRUE;
  116.  END yyIsEqual;
  117.  
  118. PROCEDURE LifeTime1 (t: Tree.tTree);
  119.  VAR yyTempo: RECORD CASE : INTEGER OF
  120.  END; END;
  121.  BEGIN
  122.   IF t = Tree.NoTree THEN RETURN; END;
  123.   IF (t^.Kind = Tree.Class) THEN
  124. (* line 71 "" *)
  125.      WITH t^.Class DO
  126. (* line 71 "" *)
  127.       
  128.  
  129.     LifeSize := InstCount + 1;
  130.     MakeArray (LifePtr, LifeSize, TSIZE (tLife));
  131.     FOR i := 1 TO InstCount DO
  132.        LifePtr^ [i].Birth := 0;
  133.        LifePtr^ [i].Death := 0;
  134.     END;
  135.     Visit := 1;
  136.     FOR i := 1 TO InstCount DO
  137.        i2 := Instance^ [i].Order;
  138.        WITH Instance^ [i2] DO
  139.           IF {Left, Inherited, First} <= Properties THEN
  140.          Visit := Attribute^.Child.Partition;
  141.  
  142.           END;
  143.           IF {Left, Synthesized} <= Properties THEN
  144.          LifePtr^ [i2].Birth := Visit;
  145.          INCL (Attribute^.Child.Usage, Visit);
  146.  
  147.           END;
  148.           IF Right IN Properties THEN
  149.          LifePtr^ [i2].Birth := Visit;
  150.  
  151.           END;
  152.           IF ({Left, Synthesized, First} <= Properties) AND NOT (Dummy IN Properties) OR
  153.          ({Right,  Inherited, First} <= Properties) THEN
  154.          FOR j := 1 TO AttrCount DO
  155.             IF IsRelated (i2, j, DP) THEN
  156.                LifePtr^ [j].Death := Visit;
  157.                INCL (Instance^ [j].Attribute^.Child.Usage, Visit);
  158.  
  159.             END;
  160.          END;
  161.          FOR j := AttrCount + 1 TO InstCount DO
  162.             IF IsRelated (i2, j, DP) THEN
  163.                LifePtr^ [j].Death := Visit;
  164.  
  165.             END;
  166.          END;
  167.           END;
  168.           IF {Right, Synthesized, First} <= Properties THEN
  169.          ActClass := t;
  170.          ActChild := Selector;
  171.          ChildsClass := Selector^.Child.Class;
  172.          ChildsVisit := Attribute^.Child.Partition;
  173.          LifeTime2 (ChildsClass);
  174.          ForallClasses (ChildsClass^.Class.Extensions, LifeTime2);
  175.           END;
  176.        END;
  177.     END;
  178.     FOR i := 1 TO AttrCount DO
  179.        WITH Instance^ [i] DO
  180.           IF (Synthesized IN Properties) AND (LifePtr^ [i].Birth < LifePtr^ [i].Death) THEN
  181.          INCL (Attribute^.Child.Properties, Tree.Tree);
  182.           END;
  183.        END;
  184.  
  185.     END;
  186.     FOR i := AttrCount + 1 TO InstCount DO
  187.        IF LifePtr^ [i].Birth < LifePtr^ [i].Death THEN
  188.           WITH Instance^ [i] DO
  189.          INCL (Attribute^.Child.Properties, Tree.Tree);
  190.           END;
  191.        END;
  192.  
  193.     END;
  194.     ReleaseArray (LifePtr, LifeSize, TSIZE (tLife));
  195. ;
  196.       RETURN;
  197.      END;
  198.  
  199.   END;
  200.  END LifeTime1;
  201.  
  202. PROCEDURE LifeTime2 (t: Tree.tTree);
  203.  VAR yyTempo: RECORD CASE : INTEGER OF
  204.  END; END;
  205.  BEGIN
  206.   IF t = Tree.NoTree THEN RETURN; END;
  207.   IF (t^.Kind = Tree.Class) THEN
  208. (* line 144 "" *)
  209.      WITH t^.Class DO
  210. (* line 144 "" *)
  211.       
  212.     v := 1;
  213.     FOR j := 1 TO InstCount DO
  214.        j2 := Instance^ [j].Order;
  215.        WITH Instance^ [j2] DO
  216.           IF {Left, Inherited, First} <= Properties THEN
  217.          v := Attribute^.Child.Partition;
  218.  
  219.          IF v > ChildsVisit THEN RETURN; END;
  220.           END;
  221.           IF NOT (Dummy IN Properties) AND (ChildsVisit = v) THEN
  222.          FOR k := 1 TO ChildsClass^.Class.AttrCount DO
  223.             IF IsRelated (j2, k, DP) THEN
  224.                LifePtr^ [ActClass^.Class.AttrCount + ActChild^.Child.InstOffset + k].Death := Visit;
  225.  
  226.             END;
  227.          END;
  228.           END;
  229.        END;
  230.     END;
  231. ;
  232.       RETURN;
  233.      END;
  234.  
  235.   END;
  236.  END LifeTime2;
  237.  
  238. PROCEDURE LifeTime3 (t: Tree.tTree);
  239.  VAR yyTempo: RECORD CASE : INTEGER OF
  240.  END; END;
  241.  BEGIN
  242.   IF t = Tree.NoTree THEN RETURN; END;
  243.   IF (t^.Kind = Tree.Class) THEN
  244. (* line 169 "" *)
  245.      WITH t^.Class DO
  246. (* line 169 "" *)
  247.       
  248.     ActClass := t;
  249.     ForallAttributes (Attributes, LifeTime3);
  250. ;
  251.       RETURN;
  252.      END;
  253.  
  254.   END;
  255.   IF (t^.Kind = Tree.Child) THEN
  256. (* line 173 "" *)
  257.      WITH t^.Child DO
  258. (* line 173 "" *)
  259.       
  260.     INCL (Properties, Tree.Tree);
  261.     IF Input IN Properties THEN
  262.        INC (ChildrenIn);
  263.     ELSE
  264.        INC (ChildrenDyn);
  265.     END;
  266. ;
  267.       RETURN;
  268.      END;
  269.  
  270.   END;
  271.   IF (t^.Kind = Tree.Attribute) THEN
  272. (* line 181 "" *)
  273.      WITH t^.Attribute DO
  274. (* line 181 "" *)
  275.       
  276.       IF IsElement (ORD ('0'), Options) THEN
  277.     IF (Input IN Properties) OR (Output IN Properties) THEN
  278.        INCL (Properties, Tree.Tree);
  279.        IF Input IN Properties THEN
  280.           INC (AttributeIn);
  281.        ELSE
  282.           INC (AttributeOut);
  283.        END;
  284.     END;
  285.     IF NOT (Tree.Tree IN Properties) THEN
  286.        INCL (Properties, Parameter);
  287.     END;
  288.       ELSE
  289.     INCL (Properties, Tree.Tree);
  290.       END;
  291.     IF {Test, Dummy, Virtual, Demand} * Properties # {} THEN
  292.        EXCL (Properties, Tree.Tree);
  293.        EXCL (Properties, Parameter);
  294.     END;
  295.       IF IsElement (ORD ('3'), Options) THEN
  296.     IF ({Test, Dummy, Virtual, Input, Output} * Properties) = {} THEN
  297.        WriteIdent    (StdOutput, ActClass^.Class.Name);
  298.        WriteS    (StdOutput, "    = ");
  299.        WriteIdent    (StdOutput, Name);
  300.        WriteS    (StdOutput, "    ");
  301.        IF Tree.Tree IN Properties THEN
  302.           WriteS    (StdOutput, "Tree"    ); INC (AttributeTree);
  303.        ELSIF Parameter IN Properties THEN
  304.           WriteS    (StdOutput, "Parameter"    ); INC (AttributeParam);
  305.        ELSIF Stack IN Properties THEN
  306.           WriteS    (StdOutput, "Stack"    ); INC (AttributeStack);
  307.        ELSIF Variable IN Properties THEN
  308.           WriteS    (StdOutput, "Variable"    ); INC (AttributeVar);
  309.        ELSIF Demand IN Properties THEN
  310.           WriteS    (StdOutput, "Demand"    ); INC (AttributeDemand);
  311.        END;
  312.        WriteNl    (StdOutput);
  313.     END;
  314.       END;
  315. ;
  316.       RETURN;
  317.      END;
  318.  
  319.   END;
  320.  END LifeTime3;
  321.  
  322. PROCEDURE BeginOptimize;
  323.  BEGIN
  324.  END BeginOptimize;
  325.  
  326. PROCEDURE CloseOptimize;
  327.  BEGIN
  328.  END CloseOptimize;
  329.  
  330. PROCEDURE yyExit;
  331.  BEGIN
  332.   IO.CloseIO; System.Exit (1);
  333.  END yyExit;
  334.  
  335. BEGIN
  336.  yyf    := IO.StdOutput;
  337.  Exit    := yyExit;
  338.  BeginOptimize;
  339. END Optimize.
  340.